package cn.iocoder.yudao.module.member.controller.app.companytemplate;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.infra.api.file.FileApi;
import cn.iocoder.yudao.module.member.controller.app.companytemplate.vo.CompanyTemplatePageReqVO;
import cn.iocoder.yudao.module.member.controller.app.companytemplate.vo.CompanyTemplateRespVO;
import cn.iocoder.yudao.module.member.controller.app.companytemplate.vo.CompanyTemplateSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.companytemplate.CompanyTemplateDO;
import cn.iocoder.yudao.module.member.service.companytemplate.CompanyTemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 App - 会员公司信息模板")
@RestController
@RequestMapping("/member/company-template")
@Validated
@Slf4j
public class AppCompanyTemplateController {

    @Resource
    private CompanyTemplateService companyTemplateService;

    @Resource
    private FileApi fileApi;

    @PostMapping("/upload-img")
    @Operation(summary = "上传图片")
    public CommonResult<String> uploadImg(@RequestParam("file") MultipartFile file) throws IOException {
        return success(fileApi.createFile(file.getBytes()));
    }

    @PostMapping("/create")
    @Operation(summary = "创建会员公司信息模板")
    public CommonResult<Long> createCompanyTemplate(@Valid @RequestBody CompanyTemplateSaveReqVO createReqVO) {
        return success(companyTemplateService.createCompanyTemplate(createReqVO));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员公司信息模板")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteCompanyTemplate(@RequestParam("id") Long id) {
        companyTemplateService.deleteCompanyTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会员公司信息模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<CompanyTemplateRespVO> getCompanyTemplate(@RequestParam("id") Long id) {
        CompanyTemplateDO companyTemplate = companyTemplateService.getCompanyTemplate(id);
        return success(BeanUtils.toBean(companyTemplate, CompanyTemplateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员公司信息模板分页")
    public CommonResult<PageResult<CompanyTemplateRespVO>> getCompanyTemplatePage(@Valid CompanyTemplatePageReqVO pageReqVO) {
        PageResult<CompanyTemplateDO> pageResult = companyTemplateService.getCompanyTemplatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CompanyTemplateRespVO.class));
    }
}